package net.zigin.epi.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.epi.demo.service.SysUserService;
import net.zigin.epi.demo.vo.JsonForResult;
import net.zigin.epi.demo.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户信息控制器类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysUserController
 * @date 2021-8-24 18:41:47
 */
@Controller
@Api(tags = "用户信息")
@RestController("/sysUser")
public class SysUserController extends BaseController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询用户信息
     *
     * @param deptId 部门编号
     * @param userId 用户编号
     * @return
     */
    @GetMapping("/getSysUserInfo")
    @ApiOperation("查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", value = "部门编号", dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户编号", dataType = "String", required = true),
    })
    public JsonForResult<SysUserVo> getSysUserInfoById(@RequestParam(value = "id") String id) {

        //1、校验请求参数
        AssertUtil.isNull(id, "用户名编号不能为空");
        SysUserVo sysUserVo = sysUserService.getSysUserInfoById(id);
        return JsonForResult.<SysUserVo>builder().data(sysUserVo).success(true).build();
    }

    /**
     * 分页查询用户信息
     *
     * @param deptId    部门编号
     * @param status    状态:0-正常 1-禁用 2-删除
     * @param pageIndex 页码
     * @param pageSize  每页的行数
     * @return
     */
    @GetMapping(value = "/getSysUserList")
    @ApiOperation("分页查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", value = "部门编号", dataType = "String", required = true),
            @ApiImplicitParam(name = "status", value = "状态:0-正常 1-禁用 2-删除", dataType = "String"),
            @ApiImplicitParam(name = "pageIndex", value = "页码,默认值1", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，默认值20", dataType = "int")
    })
    public JsonForResult<List<SysUserVo>> getSysUserList(@RequestParam("deptId") String deptId,
                                                         @RequestParam(value = "status", required = false) String status,
                                                         @RequestParam(value = "pageIndex", required = false) int pageIndex,
                                                         @RequestParam(value = "pageSize", required = false) int pageSize) {
        //1、校验请求参数
        AssertUtil.isNull(deptId, "部门编号不能为空");
        //2、获取用户列表信息
        List<SysUserVo> sysUserVoList = sysUserService.getSysUserList(deptId, status, pageIndex, pageSize);
        //3、获取行数
        int totalCount = sysUserService.getSysUserCount(deptId,status);
        //4、返回结果
        return JsonForResult.<List<SysUserVo>>builder().success(true).data(sysUserVoList).total(totalCount).build();
    }

    /**
     * 新增用户信息
     *
     * @param sysUserVo 用户信息
     * @return
     */
    @PostMapping(value = "/saveSysUser")
    @ApiOperation("新增用户信息")
    @ApiImplicitParam(name = "sysUserVo", value = "用户信息", required = true, dataType = "sysUserVo")
    public JsonForResult saveSysUser(@RequestBody SysUserVo sysUserVo) {
        AssertUtil.isNull(sysUserVo.getDeptId(), "部门编号不能为空");
        AssertUtil.isNull(sysUserVo.getMobileNo(), "手机号码不能为空");
        sysUserService.saveOrUpdateSysUser(sysUserVo);
        return JsonForResult.builder().success(true).build();
    }


    /**
     * 编辑用户信息
     *
     * @param sysUserVo 用户信息
     * @return
     */
    @PostMapping(value = "/updateSysUser")
    @ApiOperation("编辑用户信息")
    @ApiImplicitParam(name = "sysUserVo", value = "用户信息", required = true, dataType = "SysUserVo")
    public JsonForResult updateSysUser(@RequestBody SysUserVo sysUserVo) {
        //1、校验请求参数
        AssertUtil.isNull(sysUserVo, "用户不存在");
        AssertUtil.isNull(sysUserVo.getDeptId(), "部门编号不能为空");
        AssertUtil.isNull(sysUserVo.getId(), "用户ID不能为空");
        //2、更新用户信息
        sysUserService.saveOrUpdateSysUser(sysUserVo);
        return JsonForResult.builder().success(true).build();
    }
}
